Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

310
Visualizações
Code to create download link which expire using nodejs or express [frontend and backend]

I'm new here. I just started using node.js - express.js I want to add download link which expire after certain time to my website. I'm struggling to code this since a week. Anyone who knows please provide the code [frontend and backend], it will be a huge help.

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

Check this simple implementation:

You store the information of the download in a file. The filename is the download session id. The file content is the real path of the file to be downloaded.

Use these three functions to manage the life cycle of the download sessions:

    /* Gets the download file path related to a download sid */
      function getDownloadFilePath(downloadSid, callback) {
    // Get the download session file name
       var dlSessionFileName = path.join(DL_SESSION_FOLDER, downloadSid + 
        '.download');

    // Check if the download session exists
    if (!fs.existsSync(dlSessionFileName)) return callback(new 
    Error('Download does not exist'));

    // Get the file path
    fs.readFile(dlSessionFileName, function(err, data) {
      if (err) return callback(err);

    // Return the file path
     callback(null, data);
    });
   }

   /* Deletes a download session */
    function deleteDownload(downloadSid, callback) {
     // Get the download session file name
    var dlSessionFileName = path.join(DL_SESSION_FOLDER, downloadSid + '.download');

  // Check if the download session exists
  if (!fs.existsSync(dlSessionFileName)) return callback(new Error('Download does not exist'));

  // Delete the download session
  fs.unlink(dlSessionFileName, function(err) {
    if (err) return callback(err);

    // Return success (no error)
    callback();
  });
}

Use createDownload() to create download sessions wherever you need to. It returns the download sid, then you can use it to build your download URL like: http://your.server.com/download?sid=.

Finally you can add a simple handler to your /download route:

app.get('/download', function(req, res, next) {
  // Get the download sid
  var downloadSid = req.query.sid;

  // Get the download file path
  getDownloadFilePath(downloadSid, function(err, path) {
    if (err) return res.end('Error');

    // Read and send the file here...

    // Finally, delete the download session to invalidate the link
    deleteDownload(downloadSid, function(err) {
      // ...
    });
  });
});

With this method, you don't have to create/move/delete big download files, which could cause slow responses and unnecessary resource consumption.

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda